package com.shop.web.admin_banner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.annotation.Auth;
import com.shop.utils.ResultUtils;
import com.shop.utils.ResultVo;
import com.shop.web.goods.entity.Goods;
import com.shop.web.goods.service.GoodsService;
import com.shop.web.goods_category.entity.SelectType;
import com.shop.web.admin_banner.entity.BannerParm;
import com.shop.web.admin_banner.entity.AdminBanner;
import com.shop.web.admin_banner.service.AdminBannerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RequestMapping("/api/banner")
@RestController
public class AdminBannerController {
    @Autowired
    private AdminBannerService adminBannerService;
    @Autowired
    private GoodsService goodsService;

    //新增
    @PostMapping
    @Auth
    public ResultVo add(@RequestBody AdminBanner adminBanner){
        if(adminBannerService.save(adminBanner)){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑
    @PutMapping
    @Auth
    public ResultVo edit(@RequestBody AdminBanner adminBanner){
        if(adminBannerService.updateById(adminBanner)){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    //删除
    @DeleteMapping("/{banId}")
    @Auth
    public ResultVo delete(@PathVariable("banId") Long banId){
        if(adminBannerService.removeById(banId)){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

     //列表
    @GetMapping("/list")
    @Auth
    public ResultVo list(BannerParm parm){
        //构造分页对象
        IPage<AdminBanner> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<AdminBanner> query = new QueryWrapper<>();
        query.lambda().like(StringUtils.isNotEmpty(parm.getTitle()), AdminBanner::getTitle,parm.getTitle())
        .orderByDesc(AdminBanner::getOrderNum);
        IPage<AdminBanner> list = adminBannerService.page(page, query);
        return ResultUtils.success("查询成功!",list);
    }

    //商品列表
    @GetMapping("/getGoods")
    @Auth
    public ResultVo getGoods(){
        //查询商品
        QueryWrapper<Goods> query = new QueryWrapper<>();
        query.lambda().eq(Goods::getStatus,"0").eq(Goods::getSellStatus,"0");
        List<Goods> list = goodsService.list(query);
        //组装select需要的数据格式
        List<SelectType> selectList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            SelectType type = new SelectType();
            type.setValue(list.get(i).getGoodsId());
            type.setLabel(list.get(i).getGoodsName());
            selectList.add(type);
        }
        return ResultUtils.success("查询成功",selectList);
    }

    //上架下架
    @PostMapping("/upandown")
    @Auth
    public ResultVo upandown(@RequestBody AdminBanner adminBanner){
        UpdateWrapper<AdminBanner> query = new UpdateWrapper<>();
            query.lambda().set(AdminBanner::getStatus, adminBanner.getStatus())
                    .eq(AdminBanner::getBanId, adminBanner.getBanId());
        if(adminBannerService.update(query)){
            return ResultUtils.success("设置成功!");
        }
        return ResultUtils.error("设置失败!");
    }

    //查询小程序首页轮播图数据
    @GetMapping("/getIndexBanner")
    public ResultVo getIndexBanner(){
        QueryWrapper<AdminBanner> query = new QueryWrapper<>();
        query.lambda().eq(AdminBanner::getStatus,"0")
                .orderByAsc(AdminBanner::getOrderNum);
        List<AdminBanner> list = adminBannerService.list(query);
        if(list.size() > 0){
            for (int i=0;i<list.size();i++){
                Long goodsId = list.get(i).getGoodsId();
                Goods goods = goodsService.getById(goodsId);
                list.get(i).setGoods(goods);
            }
        }
        return ResultUtils.success("查询成功",list);
    }

}
